package com.sinocare.project.report.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sinocare.project.common.entity.SccMedicalResult;
import com.sinocare.project.common.result.Result;
import com.sinocare.project.common.result.ResultCode;
import com.sinocare.project.report.db.*;
import com.sinocare.project.report.service.SccMedicalResultService;
import com.sinocare.project.report.vo.SccMedicalResultVo;
import com.sinocare.project.report.vo.SccMedicalResultVo1;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

/**
 * 检测结果表(SccMedicalResult)表控制层
 *
 * @author makejava
 * @since 2021-12-21 15:52:46
 */
@RestController
@RequestMapping("sccMedicalResult")
@CrossOrigin
public class SccMedicalResultController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private SccMedicalResultService sccMedicalResultService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param data 实体json
     * @return 所有数据
     */
    @GetMapping
    public Result selectAll(Integer page, Integer limit, String data) {
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasBlank(data)) {
            SccMedicalResultVo resultVo = JSON.parseObject(data, SccMedicalResultVo.class);
            wrapper.eq(!StrUtil.hasBlank(resultVo.getHeadStore()), "c.parent_code", resultVo.getHeadStore());
            wrapper.eq(!StrUtil.hasBlank(resultVo.getSubStore()), "c.store_code", resultVo.getSubStore());
            if (resultVo.getStart() != null && resultVo.getEnd() != null) {
                wrapper.ge("a.test_time", resultVo.getStart());
                wrapper.le("a.test_time", resultVo.getEnd());
            }
        }
        PageHelper.startPage(page, limit);
        List<SccMedicalResultDo> result = this.sccMedicalResultService.getResult(wrapper);
        for (int i = 0; i < result.size(); i++) {
            Integer sbpResult = result.get(i).getSbpResult();//收缩压
            Integer dbpResult = result.get(i).getDbpResult();//舒张压
            int i1 = (sbpResult * (1 / 3)) + (dbpResult * (2 / 3));
            result.get(i).setBloodPressure(Double.parseDouble(i1 + "") + "");
        }
        PageInfo<SccMedicalResultDo> info = new PageInfo<>(result);
        return Result.ok(ResultCode.SUCCESS, info.getTotal(), info.getList());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param data 主键
     * @return 单条数据
     */
    @GetMapping("moon")
    public Result moon(String data) {
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasBlank(data)) {
            SccMedicalResultVo resultVo = JSON.parseObject(data, SccMedicalResultVo.class);
            wrapper.eq(!StrUtil.hasBlank(resultVo.getHeadStore()), "c.parent_code", resultVo.getHeadStore());
            wrapper.eq(!StrUtil.hasBlank(resultVo.getSubStore()), "c.store_code", resultVo.getSubStore());
            if (resultVo.getStart() != null && resultVo.getEnd() != null) {
                wrapper.ge("a.test_time", resultVo.getStart());
                wrapper.le("a.test_time", resultVo.getEnd());
            }
        }
        return Result.ok(ResultCode.SUCCESS, this.sccMedicalResultService.result(wrapper));
    }

    @GetMapping("YIELD")
    public Result YIELD(String data) {
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasBlank(data)) {
            SccMedicalResultVo resultVo = JSON.parseObject(data, SccMedicalResultVo.class);
            wrapper.eq(!StrUtil.hasBlank(resultVo.getHeadStore()), "a.parent_code", resultVo.getHeadStore());
            wrapper.eq(!StrUtil.hasBlank(resultVo.getSubStore()), "a.store_code", resultVo.getSubStore());
            if (resultVo.getStart() != null && resultVo.getEnd() != null) {
                wrapper.ge("c.test_time", resultVo.getStart());
                wrapper.le("c.test_time", resultVo.getEnd());
            }
        }
        NumberFormat percentInstance = NumberFormat.getPercentInstance();
        // 设置保留几位小数，这里设置的是保留两位小数
        percentInstance.setMinimumFractionDigits(2);
        List<SccMedicalResultDo2> yield = this.sccMedicalResultService.YIELD(wrapper);
        for (int i = 0; i < yield.size(); i++) {
            double belowStandard = yield.get(i).getBelowStandard() * 1.0;
            double headcount = yield.get(i).getHeadcount() * 1.0;
            double i1 = belowStandard / headcount;
            String format = percentInstance.format(i1);
            yield.get(i).setFractionDefective(format);
        }
        return Result.ok(ResultCode.SUCCESS, yield);
    }

    @GetMapping("total")
    public Result total(String data) {
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasBlank(data)) {
            SccMedicalResultVo1 resultVo = JSON.parseObject(data, SccMedicalResultVo1.class);
            wrapper.eq(!StrUtil.hasBlank(resultVo.getHeadStore()), "c.parent_code", resultVo.getHeadStore());
            wrapper.eq(!StrUtil.hasBlank(resultVo.getSubStore()), "c.store_code", resultVo.getSubStore());
            wrapper.eq(!StrUtil.hasBlank(resultVo.getTime()), "DATE_FORMAT(b.test_time,'%Y-%m')", resultVo.getTime());
        }
        return Result.ok(ResultCode.SUCCESS, this.sccMedicalResultService.total(wrapper));
    }
    @GetMapping("ranking")
    public Result ranking(String data) {
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasBlank(data)) {
            SccMedicalResultVo1 resultVo = JSON.parseObject(data, SccMedicalResultVo1.class);
            wrapper.eq(!StrUtil.hasBlank(resultVo.getHeadStore()), "a.parent_code", resultVo.getHeadStore());
            wrapper.eq(!StrUtil.hasBlank(resultVo.getSubStore()), "a.store_code", resultVo.getSubStore());
            wrapper.eq(!StrUtil.hasBlank(resultVo.getTime()), "DATE_FORMAT(b.test_time,'%Y-%m')", resultVo.getTime());
        }

        return Result.ok(ResultCode.SUCCESS, this.sccMedicalResultService.getRanking(wrapper));
    }
    @GetMapping("yc")
    public Result getYc(String data){
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasBlank(data)) {
            SccMedicalResultVo1 resultVo = JSON.parseObject(data, SccMedicalResultVo1.class);
            wrapper.eq(!StrUtil.hasBlank(resultVo.getHeadStore()), "a.parent_code", resultVo.getHeadStore());
            wrapper.eq(!StrUtil.hasBlank(resultVo.getSubStore()), "a.store_code", resultVo.getSubStore());
            wrapper.eq(!StrUtil.hasBlank(resultVo.getTime()), "DATE_FORMAT(b.test_time,'%Y-%m')", resultVo.getTime());
        }
        NumberFormat percentInstance = NumberFormat.getPercentInstance();
        // 设置保留几位小数，这里设置的是保留两位小数
        percentInstance.setMinimumFractionDigits(2);
        List<SccMedicalResultYcDo> yc = this.sccMedicalResultService.getYc(wrapper);
        for (int i = 0; i < yc.size(); i++) {
            double a=yc.get(i).getFwNumber()*1.0;
            double b=yc.get(i).getZbycNumber()*1.0;
            double obj=b/a;
            yc.get(i).setZbycResolution(percentInstance.format(obj));
        }
        return Result.ok(ResultCode.SUCCESS,yc);
    }

    @GetMapping("ycStatistics")
    public Result getYcStatistics(String data){
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasBlank(data)) {
            SccMedicalResultVo1 resultVo = JSON.parseObject(data, SccMedicalResultVo1.class);
            wrapper.eq(!StrUtil.hasBlank(resultVo.getHeadStore()), "d.parent_code", resultVo.getHeadStore());
            wrapper.eq(!StrUtil.hasBlank(resultVo.getSubStore()), "d.store_code", resultVo.getSubStore());
            wrapper.eq(!StrUtil.hasBlank(resultVo.getTime()), "DATE_FORMAT(a.test_time,'%Y-%m')", resultVo.getTime());
        }
        NumberFormat percentInstance = NumberFormat.getPercentInstance();
        // 设置保留几位小数，这里设置的是保留两位小数
        percentInstance.setMinimumFractionDigits(2);
        List<SccMedicalResultYcDataDo> ycStatistics = this.sccMedicalResultService.getYcStatistics(wrapper);
        for (int i = 0; i < ycStatistics.size(); i++) {
            double a=ycStatistics.get(i).getYcTotal()*1.0;
            double b=ycStatistics.get(i).getTotal()*1.0;
            double obj=a/b;
            ycStatistics.get(i).setYcPercentage(percentInstance.format(obj));
        }
        return Result.ok(ResultCode.SUCCESS,ycStatistics);
    }

    @GetMapping("old")
    public Result getOld(String data){
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasBlank(data)) {
            SccMedicalResultVo1 resultVo = JSON.parseObject(data, SccMedicalResultVo1.class);
            wrapper.eq(!StrUtil.hasBlank(resultVo.getHeadStore()), "d.parent_code", resultVo.getHeadStore());
            wrapper.eq(!StrUtil.hasBlank(resultVo.getSubStore()), "d.store_code", resultVo.getSubStore());
            wrapper.eq(!StrUtil.hasBlank(resultVo.getTime()), "DATE_FORMAT(b.test_time,'%Y-%m')", resultVo.getTime());
        }
        NumberFormat percentInstance = NumberFormat.getPercentInstance();
        // 设置保留几位小数，这里设置的是保留两位小数
        percentInstance.setMinimumFractionDigits(2);
        List<SccMedicalResultOldDo> old = this.sccMedicalResultService.getOld(wrapper);
        for (int i = 0; i < old.size(); i++) {
            double count=old.get(i).getTotal()*1.0;
            double a=old.get(i).getLessThanTwentyYearsOld()*1.0;
            double b=old.get(i).getTwentyToTwentyNineYearsOld()*1.0;
            double c=old.get(i).getThirtyToThirtyNineYearsOld()*1.0;
            double d=old.get(i).getFortyToFortyNineYearsOld()*1.0;
            double e=old.get(i).getFiftyToFiftyNineYearsOld()*1.0;
            double f=old.get(i).getSixtyToSixtyNineYearsOld()*1.0;
            double g=old.get(i).getOverSeventyYearsOld()*1.0;
            old.get(i).setLessThanTwentyYearsOldPercentage(percentInstance.format(a/count));
            old.get(i).setTwentyToTwentyNineYearsOldPercentage(percentInstance.format(b/count));
            old.get(i).setThirtyToThirtyNineYearsOldPercentage(percentInstance.format(c/count));
            old.get(i).setFortyToFortyNineYearsOldPercentage(percentInstance.format(d/count));
            old.get(i).setFiftyToFiftyNineYearsOldPercentage(percentInstance.format(e/count));
            old.get(i).setSixtyToSixtyNineYearsOldPercentage(percentInstance.format(f/count));
            old.get(i).setOverSeventyYearsOldPercentage(percentInstance.format(g/count));
        }
        return Result.ok(ResultCode.SUCCESS,old);

    }

    /**
     * 新增数据
     *
     * @param sccMedicalResult 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result insert(@RequestBody SccMedicalResult sccMedicalResult) {
        return Result.ok(ResultCode.SUCCESS, this.sccMedicalResultService.save(sccMedicalResult));
    }

    /**
     * 修改数据
     *
     * @param sccMedicalResult 实体对象
     * @return 修改结果
     */
    @PutMapping
    public Result update(@RequestBody SccMedicalResult sccMedicalResult) {
        return Result.ok(ResultCode.SUCCESS, this.sccMedicalResultService.updateById(sccMedicalResult));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result delete(@RequestParam("idList") List<Long> idList) {
        return Result.ok(ResultCode.SUCCESS, this.sccMedicalResultService.removeByIds(idList));
    }
}

